package cn.com.elite;

import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class SanAction extends NonSessionAction{
	private static final Log log=LogFactory.getLog(SanAction.class);
	private cn.com.elite.SanBean sanbean;
	public cn.com.elite.SanBean getSanbean() {
		return sanbean;
	}
	public void setSanbean(cn.com.elite.SanBean sanbean) {
		this.sanbean = sanbean;
	}
	private Properties readfile(String name){
		InputStream in = null;  
        Properties pros = new Properties(); 
        try{
        	if (null!=name){
        		in=SanAction.class.getResourceAsStream(name);
        		pros.load(in);
        	}
        }catch(Exception e){
        	log.error("readfile:"+e.toString());
        }
        return  pros;
	}
	private String getsql(String a,String b){
		if(a!=null&&a!=""&&a.length()>0){
			return " and "+b+"='"+a+"' ";
		}else{
			return "";
		}
	}
	
	private String getsql1(String a,String b){
		if(a!=null&&a!=""&&a.length()>0){
			String[] aa=a.split(",");
			if(aa!=null&&aa.length>0){
				String sql=" and "+b+" in (";
				for(int i=0;i<aa.length;i++){
					if(aa[i]!=null&&!aa[i].equals("")&&aa[i].length()>0){
						if(i==aa.length-1){
							sql=sql+"'"+aa[i]+"')";
						}else{
							sql=sql+"'"+aa[i]+"',";
						}
					}
				}
				return sql;
			}else{
				return "";
			}
		}else{
			return "";
		}
	}
	public ActionForward GoToMonitor(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		HttpSession session=request.getSession();
		Properties pros=readfile("sql.properties");
		if(pros!=null){
			String sql1_1_1=pros.getProperty("sql1_1_1");
			String sql1_1_2=pros.getProperty("sql1_1_2");
			String sql1_2_1=pros.getProperty("sql1_2_1");
			String sql1_2_2=pros.getProperty("sql1_2_2");
			String sql_3=pros.getProperty("sql_3");
			String sql_4=pros.getProperty("sql_4");
			String sql1_select1=pros.getProperty("sql1_select1");
			String sql2_select1=pros.getProperty("sql2_select1");
			String sql2_select2=pros.getProperty("sql2_select2");
			String sql2_select3=pros.getProperty("sql2_select3");
			String sql2_select4=pros.getProperty("sql2_select4");
		    String sql1_condition1=pros.getProperty("sql1_condition1");
		    String sql1_condition2=pros.getProperty("sql1_condition2");
		    String sql2_condition1=pros.getProperty("sql2_condition1");
		    String sql2_condition2=pros.getProperty("sql2_condition2");
		    String sql2_condition3=pros.getProperty("sql2_condition3");
		    String sql2_condition4=pros.getProperty("sql2_condition4");
		    String sql2_condition5=pros.getProperty("sql2_condition5");
		    String sql2_condition6=pros.getProperty("sql2_condition6");
		    String sql2_condition7=pros.getProperty("sql2_condition7");
		    
			
			List list1_1_1=sanbean.chaxun(sql1_1_1+"('70002','60002')");
			List list1_1_2=sanbean.chaxun(sql1_1_2);
			List list1_2_1=sanbean.chaxun(sql1_2_1+"('60002','70002')");
			List list1_2_2=sanbean.chaxun(sql1_2_2);
			List list_3=sanbean.chaxun(sql_3.replace("QUEUE_INFO","('60002','70002')")+"('60002','70002')");
			List list_4=sanbean.chaxun(sql_4);
			List list1_select1=sanbean.chaxun(sql1_select1);
			List list2_select1=sanbean.chaxun(sql2_select1);
			List list2_select2=sanbean.chaxun(sql2_select2);
			List list2_select3=sanbean.chaxun(sql2_select3);
			List list2_select4=sanbean.chaxun(sql2_select4);
			
			String sql_num_1=pros.getProperty("sql_num_1");
			List list_num_1=sanbean.chaxun(sql_num_1);
			request.setAttribute("list_num_1",list_num_1);
			
			request.setAttribute("list1_1_1",list1_1_1);
			request.setAttribute("list1_1_2",list1_1_2);
			request.setAttribute("list1_2_1",list1_2_1);
			request.setAttribute("list1_2_2",list1_2_2);
			request.setAttribute("list_3",list_3);
			request.setAttribute("list_4",list_4);
			request.setAttribute("list1_select1",list1_select1);
			request.setAttribute("list2_select1",list2_select1);
			request.setAttribute("list2_select2",list2_select2);
			request.setAttribute("list2_select3",list2_select3);
			request.setAttribute("list2_select4",list2_select4);
			
			session.setAttribute("sql1_1_1",sql1_1_1);
			session.setAttribute("sql1_1_2",sql1_1_2);
			session.setAttribute("sql1_2_1",sql1_2_1);
			session.setAttribute("sql1_2_2",sql1_2_2);
			session.setAttribute("sql_3",sql_3);
			session.setAttribute("sql_4",sql_4);
			
			session.setAttribute("sql1_condition1",sql1_condition1);
			session.setAttribute("sql1_condition2",sql1_condition2);
			session.setAttribute("sql2_condition1",sql2_condition1);
			session.setAttribute("sql2_condition2",sql2_condition2);
			session.setAttribute("sql2_condition3",sql2_condition3);
			
			session.setAttribute("sql2_condition4",sql2_condition4);
			session.setAttribute("sql2_condition5",sql2_condition5);
			session.setAttribute("sql2_condition6",sql2_condition6);
			session.setAttribute("sql2_condition7",sql2_condition7);
									
		}else{	
			return mapping.findForward("fail");
		}
		return mapping.findForward("GoToMonitor");
	}
	
	public ActionForward content1_1(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		HttpSession session=request.getSession();
		String select_num_1=request.getParameter("select_num_1");
		log.info("select_num_1:"+select_num_1);
		String[] num=select_num_1.split("-");
		String num_1="";
		if(num!=null&&num.length>0){
			for(int i=0;i<num.length;i++){
				if(i==num.length-1){
					num_1=num_1+num[i];
				}else{
					num_1=num_1+num[i]+",";
				}
			}
			num_1="("+num_1+")";
		}
		String sql=(String)session.getAttribute("sql1_1_1");
		sql=sql+getsql1(request.getParameter("condition1"),(String)session.getAttribute("sql1_condition1"));
		List chaxun_list=sanbean.chaxun(sql+num_1);
		String mReturn="";
		if(chaxun_list!=null&&chaxun_list.size()>0){
			for(int i=0;i<chaxun_list.size();i++){
				Map map=(Map)chaxun_list.get(i);
				Object content1_1_1=(Object)map.get("content1_1_1");
				Object content1_1_2=(Object)map.get("content1_1_2");
				Object content1_1_3=(Object)map.get("content1_1_3");
				Object content1_1_4=(Object)map.get("content1_1_4");
				Object content1_1_5=(Object)map.get("content1_1_5");
				Object content1_1_6=(Object)map.get("content1_1_6");
				Object content1_1_7=(Object)map.get("content1_1_7");
				Object content1_1_8=(Object)map.get("content1_1_8");
				Object content1_1_9=(Object)map.get("content1_1_9");
				if(i==chaxun_list.size()-1){
					mReturn +="{content1_1_1:'"+content1_1_1+"',content1_1_2:'"+content1_1_2+"',content1_1_3:'"+content1_1_3+"',content1_1_4:'"+content1_1_4+"',content1_1_5:'"+content1_1_5+"',content1_1_6:'"+content1_1_6+"',content1_1_7:'"+content1_1_7+"',content1_1_8:'"+content1_1_8+"',content1_1_9:'"+content1_1_9+"'}";
				}
				else{
					mReturn +="{content1_1_1:'"+content1_1_1+"',content1_1_2:'"+content1_1_2+"',content1_1_3:'"+content1_1_3+"',content1_1_4:'"+content1_1_4+"',content1_1_5:'"+content1_1_5+"',content1_1_6:'"+content1_1_6+"',content1_1_7:'"+content1_1_7+"',content1_1_8:'"+content1_1_8+"',content1_1_9:'"+content1_1_9+"'},";
				}
			}
		}
		mReturn = "["+mReturn+"]";
		response.setContentType("text/xml;charset=UTF-8"); 
		PrintWriter out = response.getWriter();
		out.println(mReturn);
		out.flush(); out.close();
		return null;
	}
	public ActionForward content1_2(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		HttpSession session=request.getSession();
		String sql=(String)session.getAttribute("sql1_1_2");
		sql=sql+getsql1(request.getParameter("condition1"),(String)session.getAttribute("sql1_condition2"));
		List chaxun_list=sanbean.chaxun(sql);
		String mReturn="";
		if(chaxun_list!=null&&chaxun_list.size()>0){
			for(int i=0;i<chaxun_list.size();i++){
				Map map=(Map)chaxun_list.get(i);
				Object content1_2_1=(Object)map.get("content1_2_1");
				Object content1_2_2=(Object)map.get("content1_2_2");
				Object content1_2_3=(Object)map.get("content1_2_3");
				Object content1_2_4=(Object)map.get("content1_2_4");
				Object content1_2_5=(Object)map.get("content1_2_5");
				Object content1_2_6=(Object)map.get("content1_2_6");
				Object content1_2_7=(Object)map.get("content1_2_7");
				Object content1_2_8=(Object)map.get("content1_2_8");
				Object content1_2_9=(Object)map.get("content1_2_9");
				Object content1_2_10=(Object)map.get("content1_2_10");
				Object content1_2_11=(Object)map.get("content1_2_11");
				if(i==chaxun_list.size()-1){
					mReturn +="{content1_2_1:'"+content1_2_1+"',content1_2_2:'"+content1_2_2+"',content1_2_3:'"+content1_2_3+"',content1_2_4:'"+content1_2_4+"',content1_2_5:'"+content1_2_5+"',content1_2_6:'"+content1_2_6+"',content1_2_7:'"+content1_2_7+"',content1_2_8:'"+content1_2_8+"',content1_2_9:'"+content1_2_9+"',content1_2_10:'"+content1_2_10+"',content1_2_11:'"+content1_2_11+"'}";
				}
				else{
					mReturn +="{content1_2_1:'"+content1_2_1+"',content1_2_2:'"+content1_2_2+"',content1_2_3:'"+content1_2_3+"',content1_2_4:'"+content1_2_4+"',content1_2_5:'"+content1_2_5+"',content1_2_6:'"+content1_2_6+"',content1_2_7:'"+content1_2_7+"',content1_2_8:'"+content1_2_8+"',content1_2_9:'"+content1_2_9+"',content1_2_10:'"+content1_2_10+"',content1_2_11:'"+content1_2_11+"'},";
				}
			}
		}
		mReturn = "["+mReturn+"]";
		response.setContentType("text/xml;charset=UTF-8"); 
		PrintWriter out = response.getWriter();
		out.println(mReturn);
		out.flush(); out.close();		
		return null;
	}
	
	public ActionForward content2_1(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		HttpSession session=request.getSession();
		String sql=(String)session.getAttribute("sql1_2_1");
		String select_num_1=request.getParameter("select_num_2");
		log.info("select_num_1:"+select_num_1);
		String[] num=select_num_1.split("-");
		String num_1="";
		if(num!=null&&num.length>0){
			for(int i=0;i<num.length;i++){
				if(i==num.length-1){
					num_1=num_1+num[i];
				}else{
					num_1=num_1+num[i]+",";
				}
			}
			num_1="("+num_1+")";
		}
		sql=sql+getsql(request.getParameter("condition1"),(String)session.getAttribute("sql2_condition1"))+getsql(request.getParameter("condition2"),(String)session.getAttribute("sql2_condition2"))+getsql(request.getParameter("condition3"),(String)session.getAttribute("sql2_condition3"));
		List chaxun_list=sanbean.chaxun(sql+num_1);
		String mReturn="";
		if(chaxun_list!=null&&chaxun_list.size()>0){
			for(int i=0;i<chaxun_list.size();i++){
				Map map=(Map)chaxun_list.get(i);
				Object content2_1_1=(Object)map.get("content2_1_1");
				Object content2_1_2=(Object)map.get("content2_1_2");
				Object content2_1_3=(Object)map.get("content2_1_3");
				Object content2_1_4=(Object)map.get("content2_1_4");
				Object content2_1_5=(Object)map.get("content2_1_5");
				Object content2_1_6=(Object)map.get("content2_1_6");
				Object content2_1_7=(Object)map.get("content2_1_7");
				Object content2_1_8=(Object)map.get("content2_1_8");
				Object content2_1_9=(Object)map.get("content2_1_9");
				Object content2_1_10=(Object)map.get("content2_1_10");
				Object content2_1_11=(Object)map.get("content2_1_11");
				Object content2_1_12=(Object)map.get("content2_1_12");
				Object content2_1_13=(Object)map.get("content2_1_13");
				if(i==chaxun_list.size()-1){
					mReturn +="{content2_1_1:'"+content2_1_1+"',content2_1_2:'"+content2_1_2+"',content2_1_3:'"+content2_1_3+"',content2_1_4:'"+content2_1_4+"',content2_1_5:'"+content2_1_5+"',content2_1_6:'"+content2_1_6+"',content2_1_7:'"+content2_1_7+"',content2_1_8:'"+content2_1_8+"',content2_1_9:'"+content2_1_9+"',content2_1_10:'"+content2_1_10+"',content2_1_11:'"+content2_1_11+"',content2_1_12:'"+content2_1_12+"',content2_1_13:'"+content2_1_13+"'}";
				}
				else{
					mReturn +="{content2_1_1:'"+content2_1_1+"',content2_1_2:'"+content2_1_2+"',content2_1_3:'"+content2_1_3+"',content2_1_4:'"+content2_1_4+"',content2_1_5:'"+content2_1_5+"',content2_1_6:'"+content2_1_6+"',content2_1_7:'"+content2_1_7+"',content2_1_8:'"+content2_1_8+"',content2_1_9:'"+content2_1_9+"',content2_1_10:'"+content2_1_10+"',content2_1_11:'"+content2_1_11+"',content2_1_12:'"+content2_1_12+"',content2_1_13:'"+content2_1_13+"'},";
				}
			}
		}
		mReturn = "["+mReturn+"]";
		response.setContentType("text/xml;charset=UTF-8"); 
		PrintWriter out = response.getWriter();
		out.println(mReturn);
		out.flush(); out.close();		
		return null;
	}
	public ActionForward content2_2(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		HttpSession session=request.getSession();
		String sql=(String)session.getAttribute("sql1_2_2");
		sql=sql+getsql(request.getParameter("condition1"),(String)session.getAttribute("sql2_condition4"))+getsql(request.getParameter("condition2"),(String)session.getAttribute("sql2_condition5"))+getsql(request.getParameter("condition3"),(String)session.getAttribute("sql2_condition6"))+getsql1(request.getParameter("condition4"),(String)session.getAttribute("sql2_condition7"));
		List chaxun_list=sanbean.chaxun(sql);
		String mReturn="";
		if(chaxun_list!=null&&chaxun_list.size()>0){
			for(int i=0;i<chaxun_list.size();i++){
				Map map=(Map)chaxun_list.get(i);
				Object content2_2_1=(Object)map.get("content2_2_1");
				Object content2_2_2=(Object)map.get("content2_2_2");
				Object content2_2_3=(Object)map.get("content2_2_3");
				Object content2_2_4=(Object)map.get("content2_2_4");
				Object content2_2_5=(Object)map.get("content2_2_5");
				Object content2_2_6=(Object)map.get("content2_2_6");
				Object content2_2_7=(Object)map.get("content2_2_7");
				Object content2_2_8=(Object)map.get("content2_2_8");
				Object content2_2_9=(Object)map.get("content2_2_9");
				if(i==chaxun_list.size()-1){
					mReturn +="{content2_2_1:'"+content2_2_1+"',content2_2_2:'"+content2_2_2+"',content2_2_3:'"+content2_2_3+"',content2_2_4:'"+content2_2_4+"',content2_2_5:'"+content2_2_5+"',content2_2_6:'"+content2_2_6+"',content2_2_7:'"+content2_2_7+"',content2_2_8:'"+content2_2_8+"',content2_2_9:'"+content2_2_9+"'}";
				}
				else{
					mReturn +="{content2_2_1:'"+content2_2_1+"',content2_2_2:'"+content2_2_2+"',content2_2_3:'"+content2_2_3+"',content2_2_4:'"+content2_2_4+"',content2_2_5:'"+content2_2_5+"',content2_2_6:'"+content2_2_6+"',content2_2_7:'"+content2_2_7+"',content2_2_8:'"+content2_2_8+"',content2_2_9:'"+content2_2_9+"'},";
				}
			}
		}
		mReturn = "["+mReturn+"]";
		response.setContentType("text/xml;charset=UTF-8"); 
		PrintWriter out = response.getWriter();
		out.println(mReturn);
		out.flush(); out.close();			
		return null;
	}	
	public ActionForward content3(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		HttpSession session=request.getSession();
		String sql=(String)session.getAttribute("sql_3");
		String select_num_1=request.getParameter("select_num_3");
		log.info("select_num_1:"+select_num_1);
		String[] num=select_num_1.split("-");
		String num_1="";
		if(num!=null&&num.length>0){
			for(int i=0;i<num.length;i++){
				if(i==num.length-1){
					num_1=num_1+num[i];
				}else{
					num_1=num_1+num[i]+",";
				}
			}
			num_1="("+num_1+")";
		}
		List chaxun_list=sanbean.chaxun(sql.replace("QUEUE_INFO",num_1)+num_1);
		String mReturn="";
		if(chaxun_list!=null&&chaxun_list.size()>0){
			for(int i=0;i<chaxun_list.size();i++){
				Map map=(Map)chaxun_list.get(i);
				Object content3_1=(Object)map.get("content3_1");
				Object content3_2=(Object)map.get("content3_2");
				Object content3_3=(Object)map.get("content3_3");
				Object content3_4=(Object)map.get("content3_4");
				Object content3_5=(Object)map.get("content3_5");
				Object content3_6=(Object)map.get("content3_6");
				Object content3_7=(Object)map.get("content3_7");
				if(i==chaxun_list.size()-1){
					mReturn +="{content3_1:'"+content3_1+"',content3_2:'"+content3_2+"',content3_3:'"+content3_3+"',content3_4:'"+content3_4+"',content3_5:'"+content3_5+"',content3_6:'"+content3_6+"',content3_7:'"+content3_7+"'}";
				}
				else{
					mReturn +="{content3_1:'"+content3_1+"',content3_2:'"+content3_2+"',content3_3:'"+content3_3+"',content3_4:'"+content3_4+"',content3_5:'"+content3_5+"',content3_6:'"+content3_6+"',content3_7:'"+content3_7+"'},";
				}
			}
		}
		mReturn = "["+mReturn+"]";
		response.setContentType("text/xml;charset=UTF-8"); 
		PrintWriter out = response.getWriter();
		out.println(mReturn);
		out.flush(); out.close();			
		return null;
	}	
	public ActionForward content4(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		HttpSession session=request.getSession();
		String sql=(String)session.getAttribute("sql_4");
		List chaxun_list=sanbean.chaxun(sql);
		String mReturn="";
		if(chaxun_list!=null&&chaxun_list.size()>0){
			for(int i=0;i<chaxun_list.size();i++){
				Map map=(Map)chaxun_list.get(i);
				Object content4_1=(Object)map.get("content4_1");
				Object content4_2=(Object)map.get("content4_2");
				Object content4_3=(Object)map.get("content4_3");
				Object content4_4=(Object)map.get("content4_4");
				Object content4_5=(Object)map.get("content4_5");
				Object content4_6=(Object)map.get("content4_6");
				Object content4_7=(Object)map.get("content4_7");
				Object content4_8=(Object)map.get("content4_8");
				Object content4_9=(Object)map.get("content4_9");
				if(i==chaxun_list.size()-1){
					mReturn +="{content4_1:'"+content4_1+"',content4_2:'"+content4_2+"',content4_3:'"+content4_3+"',content4_4:'"+content4_4+"',content4_5:'"+content4_5+"',content4_6:'"+content4_6+"',content4_7:'"+content4_7+"',content4_8:'"+content4_8+"',content4_9:'"+content4_9+"'}";
				}
				else{
					mReturn +="{content4_1:'"+content4_1+"',content4_2:'"+content4_2+"',content4_3:'"+content4_3+"',content4_4:'"+content4_4+"',content4_5:'"+content4_5+"',content4_6:'"+content4_6+"',content4_7:'"+content4_7+"',content4_8:'"+content4_8+"',content4_9:'"+content4_9+"'},";
				}
			}
		}
		mReturn = "["+mReturn+"]";
		response.setContentType("text/xml;charset=UTF-8"); 
		PrintWriter out = response.getWriter();
		out.println(mReturn);
		out.flush(); out.close();			
		return null;
	}		
}
